#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , _ui_show_img_timer(new QTimer(this))
{
    ui->setupUi(this);

    // ui界面显示camera实时画面
    connect(_ui_show_img_timer, &QTimer::timeout, this, [&](){
        auto cv_img = _controller->getCurrentCameraImageCVMat();
        if (cv_img.empty()) return;
        cv::cvtColor(cv_img, cv_img, cv::COLOR_BGR2RGB);

        auto q_img = QImage((const unsigned char*)(cv_img.data), cv_img.cols, cv_img.rows, QImage::Format_RGB888);

        QRCodeController::setQImageToLabel(q_img, ui->lb_showpix);
    });

    // 相机状态显示
    QTimer *update_timer = new QTimer(this);
    connect(update_timer, &QTimer::timeout, this, [&](){
        ui->lb_opened->setText(_controller->isCameraOpened() ? "true" : "false");
        ui->lb_detecting->setText(_controller->isDetecting() ? "true" : "false");
    });

    update_timer->start(50);

    if (_controller == nullptr) {
        _controller = new QRCodeController(ui->combob_systemCamera->currentText(), this);

        connect(_controller, &QRCodeController::detected, this, [&](QRCodeController::DetectResult result){
            if (result.empty()) return;
            _ui_show_img_timer->stop();
            QRCodeController::drawContoursOfDetectResultToLabel(result, ui->lb_showpix, true);
            ui->textb_qrcontent->clear();

            for (const auto& i : result.strs) {
                static int idx = 0;
                ui->textb_qrcontent->append(QString::number(idx++) + ": " + QString::fromStdString(i));
            }
        });
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pb_checkSystemCamera_clicked()
{
    // 检测系统camera设备
    _checkSysCamera();
}

void MainWindow::on_pb_camera_clicked(bool checked)
{

    if (checked) {
        _startCamera();
    } else {
        _controller->closeCamera();
        _ui_show_img_timer->stop();
        ui->lb_showpix->clear();
        ui->lb_showpix->setText("Not Connected");
        _checkSysCamera();
    }
}

void MainWindow::on_pb_restartDetect_clicked()
{
    _startCamera();
}

void MainWindow::_checkSysCamera()
{
    auto syscameras = QRCodeController::getAvailbleCameras();
    ui->combob_systemCamera->clear();

    for (const auto& item : syscameras) {
        ui->combob_systemCamera->addItem(item.deviceName());
    }

    ui->pb_camera->setDisabled(syscameras.empty());
    ui->pb_reopenCamera->setDisabled(syscameras.empty());
    ui->pb_restartDetect->setDisabled(syscameras.empty());
}

void MainWindow::_startCamera(bool forced)
{
    _controller->setCameraName(ui->combob_systemCamera->currentText()); // reset the new device name
    _controller->openCameraAndDetect(forced);
    if (_controller->isCameraOpened()) {
        _ui_show_img_timer->start(_timeout_msec);
    }

    ui->pb_camera->setChecked(_controller->isCameraOpened());

    // _checkSysCamera();
}

void MainWindow::on_pb_reopenCamera_clicked()
{
    _startCamera(true);
    // _checkSysCamera();
}

